package au.com.bus4u.controller.admin;


import au.com.bus4u.controller.BaseController;
import au.com.bus4u.service.AuctionService;
import au.com.bus4u.service.OrderOldService;
import au.com.bus4u.service.UserService;
import au.com.bus4u.utils.JsonResult;
import au.com.bus4u.utils.PageData;
import com.github.pagehelper.PageInfo;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/admin/orderOld")
@Log
public class GuestOrderOldController extends BaseController {
    @Autowired
    private HttpServletRequest request;
    @Resource
    private OrderOldService orderOldService;
    @Resource
    private AuctionService auctionService;
    @Resource
    private UserService userService;


    /**
     * /admin/menu/add
     */
    @ResponseBody
    @RequestMapping({"/add"})
    public JsonResult add() throws Exception {
        log.info("=========================================");
        JsonResult result = new JsonResult();
        orderOldService.save(getPageData());
        result.setCode(200);
        result.setMsg("添加成功");
        return result;
    }
    @ResponseBody
    @RequestMapping({"/del","/delete"})
    public JsonResult deleteMenu(Integer idorders) throws Exception {
        log.info("=========================================");
        JsonResult result = new JsonResult();
        orderOldService.delete(idorders);
        result.setCode(200);
        result.setMsg("删除成功");
        return result;
    }
    @ResponseBody
    @RequestMapping("/batchDelete")
    public JsonResult batchDeleteGuestOrder(String[] ids) throws Exception {
        log.info("=========================================");
        for (String id : ids){
            log.info("id = [" + id + "]");
        }
        orderOldService.deleteAll(ids);
        JsonResult result = new JsonResult();
        result.setCode(200);
        result.setMsg("删除成功");
        return result;
    }
    

    @ResponseBody
    @RequestMapping({"/update","/updateOrder"})
    public JsonResult updateMenu() throws Exception {
        log.info("=========================================");
        JsonResult result = new JsonResult();
        orderOldService.edit(getPageData());
        result.setCode(200);
        result.setMsg("修改成功");
        return result;
    }


    /**
     * 查询
     */
    @ResponseBody
    @RequestMapping("loadAllGuestOrder")
    public PageData loadAllGuestOrder() throws Exception {
        log.info("=========================================");
        setPageHelperStartPage();//从request中获取pageNum和pageSize并设置到PageHelper.startPage

        PageData pageData = getPageData();
        pageData.put("menu_type",1);
        List<PageData> list = orderOldService.list(pageData); // 列出Category列表
        PageInfo<PageData> pageInfo = new PageInfo<>(list);//得到分页的结果对象
        List<PageData> pageList = pageInfo.getList();//获取返回页面的分页list数据
        PageData page = getPage(pageInfo);//获取分页数据page

        PageData result = new PageData();
        result.put("code",0);
        result.put("data",pageList);
        result.put("page",page);
        result.put("count",page.get("total"));//总条数 layui数据表分页组件需要

        return result;
    }

    /**
     * 查询   显示该订单的竞拍信息
     */
    @ResponseBody
    @RequestMapping("showAuctionInfo")
    public PageData showAuctionInfo(Integer order_id) throws Exception {
        log.info("=========================================");
        PageData result = new PageData();
        List<PageData> auctionList = auctionService.findByOrderId(order_id);
        result.put("auctionList",auctionList);
        result.put("code",200);
        return result;
    }



    


    /**
     * 查询   显示该订单的服务人员信息(司机、承运商信息)
     * 通过订单ID查询该订单的服务人员信息
     * 如果订单为
     *      内部消化订单 管理员还未指定司机 则无服务人员信息
     *      竞拍订单 竞拍成功情况下 返回服务人员信息为 竞拍成功人的信息
     *          服务人员角色为 司机
     *          服务人员角色为 承运商
     *
     */
    @ResponseBody
    @RequestMapping("showDiverInfo")
    public PageData showDiverInfo(Integer order_id) throws Exception {
        log.info("=========================================");
        PageData result = new PageData();
        PageData servicePersonal = userService.getServicePersonal(order_id);
        result.put("servicePersonal",servicePersonal);
        result.put("code",200);
        return result;
    }
}
